package com.saleSystem.common;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;


/**
 * 用于存放异步数据接口的响应
 * @author guanwei
 *
 * @param <T>
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ServerResponse<T> implements Serializable {

	private static final long serialVersionUID = 1L;
	private int code;
	private String message;
	private T result;

	private ServerResponse(int code) {
		this.code = code;
	}

	private ServerResponse(int code, T result) {
		this.code = code;
		this.result = result;
	}

	private ServerResponse(int code, String message, T result) {
		this.code = code;
		this.message = message;
		this.result = result;
	}

	private ServerResponse(int code, String message) {
		this.code = code;
		this.message = message;
	}

	@JsonIgnore
	// 使之不在Json序列化结果之中
	public boolean isSuccess() {
		return this.code == ResponseCode.SUCCESS.getStatus();
	}

	public int getCode() {
		return code;
	}

	public String getMessage() {
		return message;
	}

	public T getResult() {
		return result;
	}

	public static <T> ServerResponse<T> createBySuccess() {
		return new ServerResponse<T>(ResponseCode.SUCCESS.getStatus());
	}

	public static <T> ServerResponse<T> createBySuccessMessage(String msg) {
		return new ServerResponse<T>(ResponseCode.SUCCESS.getStatus(), msg);
	}

	public static <T> ServerResponse<T> createBySuccess(T data) {
		return new ServerResponse<T>(ResponseCode.SUCCESS.getStatus(), data);
	}

	public static <T> ServerResponse<T> createBySuccess(String msg, T data) {
		return new ServerResponse<T>(ResponseCode.SUCCESS.getStatus(), msg, data);
	}

	public static <T> ServerResponse<T> createByError() {
		return new ServerResponse<T>(ResponseCode.ERROR.getStatus(), ResponseCode.ERROR.getDesc());
	}

	public static <T> ServerResponse<T> createByErrorMessage(String errorMessage) {
		return new ServerResponse<T>(ResponseCode.ERROR.getStatus(), errorMessage);
	}

	public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode,String errorMessage){
	        return new ServerResponse<T>(errorCode,errorMessage);
	}
   
	public static <T> ServerResponse<T> createByError(String msg, T data) {
		return new ServerResponse<T>(ResponseCode.ERROR.getStatus(), msg, data);
	}
}
